home *** CD-ROM | disk | FTP | other *** search
- /* TrackPosition.c */
- /*
- * Copyright © 1989 Martin Minow. All rights reserved.
- *
- * Convert from textual to positional coordinates.
- *
- * void
- * TrackGetPosition(dot, track_handle, hpixel, vpixel)
- * DOT dot;
- * TrackHandle track_handle;
- * LONGINT *hpixel;
- * LONGINT *vpixel;
- *
- * TrackGetPosition returns the point (in window-local
- * coordinates) of the character at the indicated
- * position. Note that the results are long integers.
- * You should not assume that the point is visible.
- *
- * LONGINT
- * TrackGetHeight(endline, startline, track_handle)
- * LONGINT endline;
- * LONGINT startline;
- * TrackHandle track_handle;
- *
- * TrackGetHeight returns the total height of all of
- * the lines in the text between and including startline
- * and endline.
- *
- * _Track_dot_to_col() determines the horizontal pixel
- * coordinate for the specified DOT.
- * _Track_dot_to_bol() locates the pixel coordinates at
- * the beginning of the line containing DOT.
- * _Track_dot_to_eol() locates the pixel coordinates at
- * the end of the line containing DOT.
- * _Track_row() determines the row (index into lineStarts)
- * containing DOT.
- * _Track_row_pixel() determines the vertical location
- * in window-local coordinates for the given row.
- */
- #include "TrackEdit.h"
- #define TR (*tr)
- static LONGINT h_pixel(TrackPtr, LONGINT, INTEGER);
- static INTEGER line_length(TrackPtr, LONGINT);
-
- /*
- * Return the window-position of dot.
- */
- void
- TrackGetPoint(dot, track_handle, hpixel, vpixel)
- DOT dot;
- TrackHandle track_handle;
- LONGINT *hpixel;
- LONGINT *vpixel;
- {
- register TrackPtr tr;
- _Track_state state;
- LONGINT row;
- INTEGER col;
-
- tr = _Track_lock(track_handle, &state);
- row = _Track_row(tr, dot);
- col = (INTEGER) (dot - TR.lineStarts[row]);
- *hpixel = h_pixel(tr, row, col);
- *vpixel = _Track_row_pixel(tr, row);
- _Track_unlock(&state);
- }
-
- /*
- * Return the height of the selection (in pixels).
- */
- LONGINT
- TrackGetHeight(end, start, track_handle)
- LONGINT end;
- LONGINT start;
- TrackHandle track_handle;
- {
- register TrackPtr tr;
- _Track_state state;
- LONGINT result;
-
- tr = _Track_lock(track_handle, &state);
- result = _Track_row_pixel(tr, _Track_row(tr, end))
- - _Track_row_pixel(tr, _Track_row(tr, start));
- _Track_unlock(&state);
- return (result);
- }
-
- /*
- * _Track_dot_to_col()
- * Compute the horizontal position of the DOT in the
- * window.
- */
- LONGINT
- _Track_dot_to_col(tr, dot)
- register TrackPtr tr;
- DOT dot;
- {
- LONGINT row;
- INTEGER col;
-
- row = _Track_row(tr, dot);
- col = (INTEGER) (dot - TR.lineStarts[row]);
- return (h_pixel(tr, row, col));
- }
-
- /*
- * _Track_dot_to_bol()
- * Compute the horizontal position of the beginning of
- * the line.
- */
- LONGINT
- _Track_dot_to_bol(tr, row)
- register TrackPtr tr;
- LONGINT row;
- {
- return (_Track_h_origin(tr, row));
- }
-
- /*
- * _Track_dot_to_eol()
- * Compute the horizontal position of the end of the line.
- */
- LONGINT
- _Track_dot_to_eol(tr, row)
- register TrackPtr tr;
- LONGINT row;
- {
- INTEGER col;
-
- col = line_length(tr, row);
- return (h_pixel(tr, row, col));
- }
-
- /*
- * _Track_row(tr, dot)
- * Return the row that contains dot. The ancient and
- * honorable binary-chop table lookup algorithm.
- */
- LONGINT
- _Track_row(tr, dot)
- register TrackPtr tr;
- DOT dot;
- {
- register LONGINT mid;
- register LONGINT low;
- register LONGINT high;
-
- low = 0;
- high = TR.nLines - 1;
- while (low <= high) {
- mid = low + (high - low) / 2;
- if (dot < TR.lineStarts[mid])
- high = mid - 1;
- else if (dot >= TR.lineStarts[mid + 1])
- low = mid + 1;
- else {
- return (mid);
- }
- }
- /*
- * Don't return beyond the last value.
- */
- if (low >= TR.nLines && TR.nLines > 0)
- low = TR.nLines - 1;
- return (low);
- }
-
- LONGINT
- _Track_row_pixel(tr, row)
- register TrackPtr tr;
- LONGINT row;
- {
- return (
- (row * TR.lineHeight)
- + TR.viewRect.top /* To window-local space */
- + TR.fontAscent /* To character origin */
- - TR.topPixel /* Offset by scrolling */
- );
- }
-
- /*
- * h_pixel()
- * Compute the horizontal pixel position in window-local
- * coordinates of the specified [row, col].
- */
- LONGINT
- h_pixel(tr, row, col)
- register TrackPtr tr;
- LONGINT row;
- INTEGER col;
- {
- LONGINT pixel;
- Ptr line_start;
- INTEGER state;
-
- state = HGetState(TR.hText);
- HLock(TR.hText);
- line_start = (*TR.hText) + TR.lineStarts[row];
- pixel = TextWidth(line_start, 0, col)
- + _Track_h_origin(tr, row);
- HSetState(TR.hText, state);
- return (pixel);
- }
-
- /*
- * _Track_h_origin()
- * Compute the horizontal pixel position in window-local
- * coordinates of the first (i.e., leftmost) character
- * in this row. This is the only function that
- * knows about text justification.
- */
- LONGINT
- _Track_h_origin(tr, row)
- register TrackPtr tr;
- LONGINT row;
- {
- LONGINT pixel;
- INTEGER window_width;
- INTEGER text_width;
- INTEGER state;
-
- pixel = TR.viewRect.left - TR.leftPixel;
- if (TR.just != 0) { /* Not left justified? */
- window_width = TR.viewRect.right - TR.viewRect.left;
- state = HGetState(TR.hText);
- HLock(TR.hText);
- text_width = TextWidth(
- (*TR.hText) + TR.lineStarts[row],
- 0,
- line_length(tr, row)
- );
- HSetState(TR.hText, state);
- if (TR.just < 0) /* Right justified */
- pixel += (window_width - text_width);
- else { /* Center justified */
- pixel += ((window_width - text_width) / 2);
- }
- }
- return (pixel);
- }
-
- /*
- * line_length(tr, row)
- * Return the number of characters in the specified row.
- */
- static int
- line_length(tr, row)
- register TrackPtr tr;
- LONGINT row;
- {
- if (row >= TR.nLines)
- return (0);
- return (TR.lineStarts[row + 1] - TR.lineStarts[row]);
- }
-
-